/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.session.wizard;

import net.zarubsys.commons.utils.StringUtils;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * NewSetupGeneralPage
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class NewSessionGeneralPage extends WizardPage {

	private Text nameWidget;
	private Text descriptionWidget;
	private String name;
	private String description;

	public NewSessionGeneralPage(String pageName) {
		super(pageName);
	}

	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(1, false));
		
		GridData gd;
		Label label;
		
		label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false));
		label.setText("Name");
		
		nameWidget = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
		gd = new GridData(SWT.BEGINNING, SWT.CENTER, true, false);
		gd.widthHint = 450;
		nameWidget.setLayoutData(gd);
		nameWidget.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				validate();
				name = nameWidget.getText();
			}
			
		});
		
		label = new Label(composite, SWT.NONE);
		gd = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
		label.setLayoutData(gd);
		label.setText("Description");
		
		descriptionWidget = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER);
		gd = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
		gd.widthHint = 450;
		gd.heightHint = 100;
		descriptionWidget.setLayoutData(gd);
		descriptionWidget.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				description = descriptionWidget.getText();
			}
			
		});
		
		setControl(composite);
		validate();
	}
	
	private void validate() {
		if (StringUtils.isNullOrEmpty(nameWidget.getText())) {
			setErrorMessage("Name is required");
			setPageComplete(false);
			return;
		}
		setErrorMessage(null);
		setPageComplete(true);
	}
	
	public String getFilledName() {
		return name;
	}
	
	public String getFilledDescription() {
		return description;
	}

}

